package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.mapper.dishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.impl.DishServicelmpl;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品接口")
public class DishController {
    @Autowired
    private DishServicelmpl dishServicelmpl;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private dishMapper dishMapper;


    @PostMapping
    @ApiOperation("菜品添加")
    public Result addDish(@RequestBody DishDTO dishDTO) {
        //添加菜品要操作两个表，首先要将新菜品添加进入菜品表，然后将口味添加进入菜品口味表
        dishServicelmpl.addDish(dishDTO);
        //删除对应的缓存
        deleteCache("dish_" + dishDTO.getCategoryId());
        return Result.success();
    }

    @GetMapping("/page")
    @CrossOrigin(origins = "http://localhost:63342")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> getDishPage(DishPageQueryDTO dishPageQueryDTO) {
        PageResult page = dishServicelmpl.page(dishPageQueryDTO);
        return Result.success(page);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id返回菜品信息")
    public Result<DishVO> getDishById(@PathVariable String id) {
        DishVO select = dishServicelmpl.select(Integer.parseInt(id));
        return Result.success(select);
    }

    @PutMapping
    @ApiOperation("菜品修改")
    public Result updateDish(@RequestBody DishDTO dishDTO) {
        dishServicelmpl.updateDish(dishDTO);
        //全部删除
        deleteCache("dish_*");
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("批量删除")
    public Result deleteDish(String ids) {
        log.info("删除......");
        dishServicelmpl.deleteById(ids);
        //全部删除
        deleteCache("dish_*");
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("设置菜品销售状态")
    public Result setStatus(@PathVariable Integer status, Integer id) {
        dishServicelmpl.status(id, status);
        //全部删除
        deleteCache("dish_*");
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据菜品分类来查找菜品")
    public Result<List<Dish>> getDishList(Integer categoryId) {
        log.info(categoryId.toString());
        List<Dish> dishes = dishMapper.selectByCategoryId(categoryId);
        return Result.success(dishes);
    }

    /**
     * 菜品增删改操作时需要清理缓存
     *
     * @param param
     */
    private void deleteCache(String param) {
        Set keys = redisTemplate.keys(param);
       /* for (Object key : keys) {
            System.out.println(redisTemplate.opsForValue().get(key));
        }*/
        redisTemplate.delete(keys);

    }

}
